package com.turbo.cloud.ums.web.webservice;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.turbo.cloud.core.api.R;
import com.turbo.cloud.core.api.Result;
import com.turbo.cloud.core.base.PageQuery;
import com.turbo.cloud.core.validated.InsertValidated;
import com.turbo.cloud.core.validated.UpdateValidated;
import com.turbo.cloud.ums.web.client.OrderCilent;
import com.turbo.cloud.ums.web.logic.UserLogic;
import com.turbo.cloud.ums.web.model.user.UmsUserPwdQO;
import com.turbo.cloud.ums.web.model.user.UmsUserQO;
import com.turbo.cloud.ums.web.model.user.UmsUserTokenVO;
import com.turbo.cloud.ums.web.model.user.UmsUserVO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2022-08-13
 */
@RequiredArgsConstructor
@Slf4j
@RestController
public class UmsUserController {

    final UserLogic userLogic;
    final OrderCilent orderCilent;

    /**
     * 模糊查询用户分页列表
     *
     * @param pageQuery 分页查询实体
     * @return Page
     */
    @PostMapping(name = "模糊查询用户分页列表", value = "/webservice/user/queryUserPage")
    public Result<Page<UmsUserVO>> queryUserPage(@RequestBody PageQuery<UmsUserQO> pageQuery) {
        PageQuery<UmsUserVO> result = userLogic.queryUserPage(pageQuery);
        return R.result(result);
    }

    /**
     * 用户登录
     *
     * @param umsUserQO 用户登录实体
     * @param request   请求实体
     * @return UmsUserTokenVO
     */
    @PostMapping(name = "用户登录", value = "/admin/login")
    public Result<UmsUserTokenVO> login(@RequestBody UmsUserQO umsUserQO, HttpServletRequest request) {
        return R.result(userLogic.login(umsUserQO, request));
    }

    /**
     * 用户登录
     *
     * @param umsUserQO 用户登录实体
     * @param request   请求实体
     * @return UmsUserTokenVO
     */
    @PostMapping(name = "用户登录", value = "/admin/loginByValidStrength")
    public Result<UmsUserTokenVO> loginByValidStrength(@RequestBody @Validated UmsUserQO umsUserQO, HttpServletRequest request) {
        return R.result(userLogic.loginByValidStrength(umsUserQO, request));
    }

    /**
     * 用户登出
     *
     * @return
     */
    @PostMapping(name = "用户登出", value = "/admin/logout")
    public Result login() {
        userLogic.logout();
        return R.success();
    }

    /**
     * 查询当前登录人信息
     *
     * @return Result<UmsUserVO>
     */
    @GetMapping(name = "查询当前登录人信息", value = "/webservice/user/queryCurrentUserInfo")
    public Result<UmsUserVO> queryCurrentUserInfo() {
        return R.result(userLogic.queryCurrentUserInfo());
    }

    /**
     * 验证用户名
     *
     * @param umsUserQO 实体
     * @return
     */
    @PostMapping(name = "验证用户名", value = "/webservice/user/verifyUsername")
    public Result<Boolean> verifyUsername(@RequestBody UmsUserQO umsUserQO) {
        return R.result(userLogic.verifyUsername(umsUserQO));
    }

    /**
     * 新增用户
     *
     * @param umsUserQO 实体
     * @return Result<Boolean>
     */
    @PostMapping(name = "新增用户", value = "/webservice/user/createUser")
    public Result<Boolean> createUser(@RequestBody @Validated(InsertValidated.class) UmsUserQO umsUserQO) {
        boolean result = userLogic.createUser(umsUserQO);
        return R.result(result);
    }

    /**
     * 修改用户
     *
     * @param umsUserQO 实体
     * @return Boolean
     */
    @PostMapping(name = "修改用户", value = "/webservice/user/modifyUser")
    public Result<Boolean> modifyUser(@RequestBody @Validated(UpdateValidated.class) UmsUserQO umsUserQO) {
        boolean result = userLogic.modifyUser(umsUserQO);
        return R.result(result);
    }

    /**
     * 删除用户
     *
     * @param id 用户ID
     * @return Boolean
     */
    @GetMapping(name = "删除用户", value = "/webservice/user/removeUserById")
    public Result<Boolean> removeUserById(@NotBlank(message = "用户ID不能为空") String id) {
        boolean result = userLogic.removeUserById(id);
        return R.result(result);
    }

    /**
     * 修改用户密码
     *
     * @param umsUserPwdQO 实体
     * @return Result<Boolean>
     */
    @PostMapping(name = "修改用户密码", value = "/webservice/user/modifyUserPwd")
    public Result<Boolean> modifyUser(@RequestBody @Validated(UpdateValidated.class) UmsUserPwdQO umsUserPwdQO) {
        boolean result = userLogic.modifyUserPwd(umsUserPwdQO);
        return R.result(result);
    }


    /**
     * 测试接口
     *
     * @return Result<Boolean>
     */
    @PostMapping(name = "测试接口", value = "/admin/user/test")
    public Result<String> adminTest() {
        String snowId = IdUtil.getSnowflakeNextIdStr();
        Snowflake snowflake = new Snowflake(1,1);
        String snowId2 = snowflake.nextIdStr();
        return R.result(snowId + "|" + snowId2);
    }
}
